#!/usr/bin/env python3
import os
import sys

# 尝试不同的输入法模块
input_methods = ['fcitx', 'ibus', 'xim']
for im in input_methods:
    os.environ['QT_IM_MODULE'] = im
    os.environ['XMODIFIERS'] = f'@im={im}'
    print(f"尝试输入法模块: {im}")
    
    from PyQt6.QtWidgets import QApplication, QMainWindow, QLineEdit, QVBoxLayout, QWidget, QLabel
    from PyQt6.QtCore import Qt
    
    app = QApplication(sys.argv)
    
    window = QMainWindow()
    window.setWindowTitle(f"测试输入法: {im}")
    window.resize(400, 200)
    
    central = QWidget()
    layout = QVBoxLayout()
    
    label = QLabel(f"正在测试: {im}\n按Ctrl+Space切换输入法")
    layout.addWidget(label)
    
    input_field = QLineEdit()
    input_field.setPlaceholderText(f"测试 {im} 输入法...")
    input_field.setAttribute(Qt.WidgetAttribute.WA_InputMethodEnabled, True)
    layout.addWidget(input_field)
    
    central.setLayout(layout)
    window.setCentralWidget(central)
    window.show()
    
    # 运行5秒后测试下一个
    from PyQt6.QtCore import QTimer
    QTimer.singleShot(5000, app.quit)
    
    app.exec()
    
    response = input(f"'{im}' 能否输入中文? (y/n): ")
    if response.lower() == 'y':
        print(f"✅ 成功使用 {im}")
        break
    else:
        print(f"❌ {im} 失败")